//
//  TieBaViewController.m
//  1gene
//
//  Created by 1jiyin on 16/3/11.
//  Copyright © 2016年 1jiyin. All rights reserved.
//

#import "TieBaViewController.h"
#import "SendTopicViewController.h"
#import "NSDictionary+NULLToNSString.h"
#import "HotAndGreatViewController.h"
@interface TieBaViewController ()<TieBaTableViewDelegate>

@property(nonatomic,copy)NSDictionary *allTopicMessageDataDic;

@property(nonatomic,copy)NSArray *allGongGaoMessageDataArray;


@end

@implementation TieBaViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self.view addSubview:[UIView new]];
    [self createNavigation];
    self.view.backgroundColor=[UIColor whiteColor];
    [self createUI];
    // Do any additional setup after loading the view.
}
-(void)createNavigation
{
    self.navigationController.navigationBar.hidden=NO;
    self.navigationController.navigationBar.barTintColor=[UIColor colorWithRed:84/255.0 green:199/255.0 blue:222/255.0 alpha:1];
    //返回
    UIButton *leftBtn=[UIButton buttonWithType:UIButtonTypeCustom];
    leftBtn.frame=CGRectMake(0, 0, 25, 25);
    [leftBtn setBackgroundImage:[UIImage imageNamed:@"left_back"] forState:UIControlStateNormal];
    [leftBtn addTarget:self action:@selector(leftBtnClick:) forControlEvents:UIControlEventTouchUpInside];
    self.navigationItem.leftBarButtonItem=[[UIBarButtonItem alloc]initWithCustomView:leftBtn];
    
    //标题
    UILabel *title=[[UILabel alloc]initWithFrame:CGRectMake(0, 0, 100, 44)];
    title.text=@"贴吧";
    title.font=[UIFont systemFontOfSize:22];
    title.textAlignment=NSTextAlignmentCenter;
    title.textColor=[UIColor whiteColor];
    self.navigationItem.titleView=title;
}

#pragma mark 返回上一页
-(void)leftBtnClick:(UIButton *)sender
{
    self.tabBarController.tabBar.hidden=NO;
    self.navigationController.navigationBar.hidden=YES;
    [self.navigationController popViewControllerAnimated:YES];
}
-(void)createUI
{
    TieBaTableView *table=[[TieBaTableView alloc]initWithFrame:CGRectMake(0, 20+44, self.view.frame.size.width, self.view.frame.size.height-20-44) style:UITableViewStyleGrouped hideheaderView:NO];
    table.imgDelegate=self;
    [self.view addSubview:table];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
//TieBaTableView的代理
-(void)bigImage:(NSInteger)currentIndex AndImageArrays:(NSArray *)imageArrays
{
    CommunityBigImageViewController *vc=[[CommunityBigImageViewController alloc]init];
   // vc.image=img;
    vc.navigationBarState=1;
    vc.imageArrays=[NSArray arrayWithArray:imageArrays];
    vc.currentIndex=currentIndex;
    [self.navigationController pushViewController:vc animated:YES];
}
-(void)gotoXiangQingViewController:(NSInteger)index
{
    TieBaXiangQingViewController *vc=[[TieBaXiangQingViewController alloc]init];
    //vc.arr=arr;
    vc.topic_id=index;
    NSLog(@"topic_id1======%ld",index);
    vc.fromWhere=@"tieba";
    [self.navigationController pushViewController:vc animated:YES];
}
-(void)chooseFunction:(NSInteger)index
{
    //NSLog(@"********=====%ld",index);
    if (index==1) {
        //公告
        GongGaoViewController *vc=[[GongGaoViewController alloc]init];
        vc.gonggaoDataArray=[NSArray arrayWithArray:_allGongGaoMessageDataArray];
        [self.navigationController pushViewController:vc animated:YES];
    }else if (index==2){
        //新消息
        NewMessageViewController *vc=[[NewMessageViewController alloc]init];
        vc.dataDic=[NSDictionary dictionaryWithDictionary:_allTopicMessageDataDic];
        [self.navigationController pushViewController:vc animated:YES];
    }else if (index==3){
        //热门话题
        CircleViewController *vc=[[CircleViewController alloc]init];
        vc.biaoqianState=1;
        [self.navigationController pushViewController:vc animated:YES];
    }else if (index==4){
        //热门帖子
        HotAndGreatViewController *vc=[[HotAndGreatViewController alloc]init];
        vc.myTitle=@"热门帖子";
        vc.selectedIndex=2;
        [self.navigationController pushViewController:vc animated:YES];
    }else if (index==5){
        //精华帖
        HotAndGreatViewController *vc=[[HotAndGreatViewController alloc]init];
        vc.myTitle=@"精华帖";
        vc.selectedIndex=3;
        [self.navigationController pushViewController:vc animated:YES];
    }else if (index==6){
        //我要发帖
        SendTopicViewController *vc=[[SendTopicViewController alloc]init];
        [self.navigationController pushViewController:vc animated:YES];
    }else if (index==7){
        //我的收藏
        MyCollectionViewController *vc=[[MyCollectionViewController alloc]init];
        [self.navigationController pushViewController:vc animated:YES];
    }else if (index==8){
        //我关注的
//        SelfViewController *vc=[[SelfViewController alloc]init];
//        [self.navigationController pushViewController:vc animated:YES];
        TieZiViewController *vc=[[TieZiViewController alloc]init];
        vc.biaoti=@"我关注的";
        [self.navigationController pushViewController:vc animated:YES];
        
    }
}
//进入用户详情界面
-(void)gotoMyAttentionViewController:(NSString *)userId
{
    NSUserDefaults *user=[NSUserDefaults standardUserDefaults];
    NSInteger currentUserId=[[user objectForKey:@"userId"] integerValue];
    NSInteger headUserId=[userId integerValue];
    if (currentUserId == headUserId) {
        SelfViewController *vc=[[SelfViewController alloc]init];
        self.tabBarController.tabBar.hidden=YES;
        vc.userId=headUserId;
        vc.barState=1;
        [self.navigationController pushViewController:vc animated:YES];
    }else{
        //我关注的
        MyAttentionViewController *vc=[[MyAttentionViewController alloc]init];
        vc.barState=1;
        vc.userId=userId;
        [self.navigationController pushViewController:vc animated:YES];
    }
    
   
}

-(void)viewWillAppear:(BOOL)animated
{
    self.navigationController.navigationBar.hidden=NO;
    self.tabBarController.tabBar.hidden=YES;
    [self getAllTopicData];
    [self getAllGongGao];
}

//获取帖子未读消息
-(void)getAllTopicData
{
    AFHTTPRequestOperationManager *manager=[AFHTTPRequestOperationManager manager];
    manager.requestSerializer=[AFHTTPRequestSerializer serializer];
    manager.responseSerializer=[AFHTTPResponseSerializer serializer];
    
    NSUserDefaults *user=[NSUserDefaults standardUserDefaults];
    NSString *token=[user objectForKey:@"token"];
    
    
    [manager GET:[NSString stringWithFormat:GetAllTopicMessage,token] parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
        NSDictionary *dic=[NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingMutableContainers error:nil];
        NSLog(@"帖子未读消息===%@数量=======%@",dic,dic[@"total"]);
        if (dic) {
            _allTopicMessageDataDic=[NSDictionary changeType:dic];
            [[NSNotificationCenter defaultCenter]postNotificationName:@"TopicMessageNumber" object:nil userInfo:@{@"number":[NSString stringWithFormat:@"%@",dic[@"total"]]}];
        }
        
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        NSLog(@"error%@",error.localizedDescription);
    }];
}

//获取所有公告
-(void)getAllGongGao
{
    AFHTTPRequestOperationManager *manager=[AFHTTPRequestOperationManager manager];
    manager.requestSerializer=[AFHTTPRequestSerializer serializer];
    manager.responseSerializer=[AFHTTPResponseSerializer serializer];
    
    NSUserDefaults *user=[NSUserDefaults standardUserDefaults];
    NSString *token=[user objectForKey:@"token"];
    
    [manager GET:[NSString stringWithFormat:AllgongGaoMessage,token] parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
        NSArray *arr=[NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingMutableContainers error:nil];
        NSLog(@"公告未读消息===%@",arr);
        if (arr) {
            _allGongGaoMessageDataArray=[NSArray arrayWithArray:arr];
            [[NSNotificationCenter defaultCenter]postNotificationName:@"GongGaoMessageNumber" object:nil userInfo:@{@"number":[NSString stringWithFormat:@"%ld",arr.count]}];
        }
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        NSLog(@"error%@",error.localizedDescription);
    }];
    

}









/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
